<template>
  <div class="container">
    <div class="header">
      <div class="title">公司详情</div>
    </div>
    <el-divider/>
    <div class="form-container">
      <el-form :model="form" label-width="120px" v-loading="loading">
        <el-form-item label="编号">
          <el-input v-model="form.CompanyCode" size="medium"/>
        </el-form-item>
        <el-form-item label="名称">
          <el-input v-model="form.UniversityName" size="medium"/>
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="form.Address" size="medium"/>
        </el-form-item>
        <el-form-item label="客服电话">
          <el-input v-model="form.TelePhone" size="medium"/>
        </el-form-item>
        <el-form-item label="经度">
          <el-input v-model="form.Longitude" size="medium"/>
        </el-form-item>
        <el-form-item label="纬度">
          <el-input v-model="form.Latitude" size="medium"/>
        </el-form-item>
        <el-form-item label="logo">
          <upload-imgs ref="uploadEle" :value="[{ display: form.Logo }]" :max-num="1"/>
        </el-form-item>
        <el-form-item label="内容">
          <tinymce-editor :default-content="form.Describe" @change="change"/>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">提交</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import Company from '@/model/company'
import UploadImgs from '@/components/base/upload-image/index'
import TinymceEditor from '@/components/base/tinymce/index'

export default {
  name: 'company-detail',
  components: {
    TinymceEditor,
    UploadImgs
  },
  data () {
    return {
      form: {},
      loading: false,
    }
  },
  created () {
    this.getDetail()
  },
  methods: {
    async getDetail () {
      try {
        this.loading = true
        const detail = await Company.getDetail()
        this.loading = false
        this.form = detail
      } catch (e) {
        this.loading = false
      }

    },
    change (val) {
      this.form.Describe = val
    },
    async onSubmit () {
      try {
        const uploadData = await this.$refs.uploadEle.getValue()
        const data = {}
        data.UniversityName = this.form.UniversityName,
          data.Describe = this.form.Describe,
          data.Address = this.form.Address,
          data.TelePhone = this.form.TelePhone,
          data.Longitude = this.form.Longitude,
          data.Latitude = this.form.Latitude
        if (uploadData.length !== 0) {
          data.Logo = uploadData[0].display
        }
        this.loading = true
        await Company.updata(data)
        this.loading = false
        this.$message.success('更新成功')
        await this.getDetail()
      } catch (e) {
        this.loading = false
      }

    },
  }
}
</script>

<style lang="scss" scoped>
.container {
  padding: 20px;

  .title {
    font-size: 16px;
    font-weight: 500;
    color: $theme;
  }

  .form-container {
    max-width: 800px;
  }
}
</style>
